<?php
defined('_JEXEC') or die('Restricted access');

require_once(JPATH_COMPONENT.DS.'persistance'.DS.'DAOLangQa.php');
require_once(JPATH_COMPONENT.DS.'domain'.DS.'DTOLangQa.php');
require_once(JPATH_COMPONENT.DS.'domain'.DS.'DTOQa.php');
require_once(JPATH_COMPONENT.DS.'domain'.DS.'DTOLang.php');

class DAOLangQaImpl implements DAOLangQa{
	
	public function addLangQa($con, $dtoLangQa){
		if(!$this->checkLangQa($con, $dtoLangQa->getLang()->getIsoCode(), $dtoLangQa->getQa()->getId())){
			$data = new stdClass();
			$data->iso_code = $dtoLangQa->getLang()->getIsoCode();
			$data->id_qa = $dtoLangQa->getQa()->getId();
			$data->q_spelling = $dtoLangQa->getQSpelling();
			$data->q_pronoun = $dtoLangQa->getQPronoun();
			$data->q_video_path = $dtoLangQa->getQVideoPath();
			$data->a_spelling = $dtoLangQa->getASpelling();
			$data->a_pronoun = $dtoLangQa->getAPronoun();
			$data->a_video_path = $dtoLangQa->getAVideoPath();
			$data->active = $dtoLangQa->isActive();

			if(!$con->insertObject( '#__hizkuntzak_lang_qas', $data)){
				echo $con->stderr();
				return false;
			}
			else 
				return true;	
		}
		else{
			
			return $this->updateLangQa($con, $dtoLangQa);
		}
		return false;
	}
	
	public function updateLangQa($con, $dtoLangQa){
		
		$sql = "UPDATE #__hizkuntzak_lang_qas SET q_spelling='$dtoLangQa->q_spelling', ";
		$sql = $sql."q_pronoun='$dtoLangQa->q_pronoun', q_video_path='$dtoLangQa->q_videoPath', ";
		$sql = $sql."a_spelling='$dtoLangQa->a_spelling', a_pronoun='$dtoLangQa->a_pronoun', a_video_path='$dtoLangQa->a_videoPath', ";
		$sql = $sql."active=$dtoLangQa->active WHERE iso_code='".$dtoLangQa->lang->iso_code."' and id_qa=".$dtoLangQa->qa->id;

		$con->setQuery($sql);
		
		if(!$con->query()){
			$con->stderr();
			return false;
		}
		else
			return true;
	}
	
	public function checkLangQa($con, $lang_code, $idQa){
		$sql = "select * from #__hizkuntzak_lang_qas where (iso_code='$lang_code' AND id_qa=$idQa)";
		
		$con->setQuery($sql);
		
    	$row = $con->loadObject();
    	
    	if($row == null || $row == ""){
    		return false;
    	}
    	return true;
	}
	
	public function getLangQa($con, $lang_code, $active){
		$results = array();
		
		$sql = "SELECT #__hizkuntzak_lang_qas . * , #__hizkuntzak_qas.q_es, #__hizkuntzak_qas.q_eu, #__hizkuntzak_qas.a_es, #__hizkuntzak_qas.a_eu, #__hizkuntzak_languages.name_es, #__hizkuntzak_languages.name_eu
			FROM #__hizkuntzak_qas
			LEFT JOIN #__hizkuntzak_lang_qas ON #__hizkuntzak_lang_qas.id_qa = #__hizkuntzak_qas.id
			LEFT JOIN #__hizkuntzak_languages ON #__hizkuntzak_lang_qas.iso_code = #__hizkuntzak_languages.iso_code
			WHERE #__hizkuntzak_lang_qas.iso_code='$lang_code'";
		
		if($active){
			$sql = $sql. "AND #__hizkuntzak_lang_qas.active=1 ";
		}

		$con->setQuery($sql);
		
    	$objectList = $con->loadObjectList();

        foreach($objectList as $row) {
        	
        	$langQa = new DTOLangQa();
        	
        	$langQa->getLang()->setIsoCode($row->iso_code);
        	$langQa->getLang()->setNameEs($row->name_es);
        	$langQa->getLang()->setNameEu($row->name_eu); 
        	$langQa->getQa()->setId($row->id_qa);
        	$langQa->getQa()->setQEs($row->q_es);
        	$langQa->getQa()->setQEu($row->q_eu);
        	$langQa->setQSpelling($row->q_spelling);
        	$langQa->setQPronoun($row->q_pronoun);
        	$langQa->setQVideoPath($row->q_video_path);
        	$langQa->getQa()->setAEs($row->a_es);
        	$langQa->getQa()->setAEu($row->a_eu);
        	$langQa->setASpelling($row->a_spelling);
        	$langQa->setAPronoun($row->a_pronoun);
        	$langQa->setAVideoPath($row->a_video_path);
        	$langQa->setActive($row->active);
            
        	$results[] = $langQa;
          } 
		
		return $results;
	}
}